package com.recommender;

import java.sql.ResultSet;
import java.util.*;

import textanalyzer.MainAnalyzer;

import Neighborhood.Neighborhood;

import com.model.DataModel;
import com.model.TagBasedUserDataModel;
import com.similarity.ContextSimilarity;
import com.similarity.TagBasedUserSimilarity;
import com.similarity.TagSimilarity;
import com.similarity.UserSimilarity;

public class UserAwareTagBasedRecommender implements Recommender{
	///ContextSimilarity tSim;
	TagBasedUserDataModel tagBasedModel;
	TagBasedUserSimilarity uSim;
	Neighborhood neighborhood;
	TagSimilarity tagSimilarity;
	
	@Override
	public Map<Integer, Double> getRecommendations(
		HashMap<String, String> contextMap, String keyword, long userID,
		DataModel datamodel,Neighborhood neighborhood) {
		
		ResultSet tuples=null;
		tagBasedModel=(TagBasedUserDataModel)datamodel;
		this.neighborhood=neighborhood;
		String tagSetString=contextMap.get("tags");
		System.out.println(tagSetString);
		uSim=new TagBasedUserSimilarity(tagBasedModel);
		tagSimilarity=new TagSimilarity(tagBasedModel);
		boolean inserted=false;
		HashMap<Integer, Double> myHashMap = new HashMap<Integer,Double>();
		//Map<Integer, Double> imageResultsMap=new Tr<Integer, Double>();
		Map<Integer, Double> imageResultsMap;
		try {
			tuples=tagBasedModel.getTuplesFilteredByKeyword(keyword);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int tmimageID=-1;
		
		HashMap<String,Integer> tagVector = new LinkedHashMap<String, Integer>(); 
		MainAnalyzer nAnalyzer = new MainAnalyzer();
		tagVector = nAnalyzer.identifyContext(tagSetString);
		
		Set t=tagVector.entrySet();
		Iterator k= t.iterator();
		String[] tagArray=new String[5];
		int j=0;
		while(k.hasNext()){
			if(j==5)
				break;
			j++;
			Map.Entry<String, Integer> me=(Map.Entry<String, Integer>)k.next();
			tagArray[j-1]=me.getKey();
				System.out.println(me.getKey().toString()+"======"+me.getValue().toString());
		}
		int tempImageID=-1;
		Long tempUserID=null;
		String tagSet="";
		HashMap<Integer, String> tagset=new HashMap<Integer, String>();

		double tempPredictedRanks=0.0f;
		double predictedRankWithTags=0.0f;
		double predictedUserSimilarity=0.0f;
		int imageID=0;
		Long longOtherUserID=null;
		HashMap<String, Integer> TagMap=null;
		try {
			while(tuples.next())
			{
				imageID=tuples.getInt("ImageID");
				longOtherUserID=Long.parseLong(tuples.getString("UserID"));
				if((tempImageID!=imageID)|| ((tempUserID!=longOtherUserID))){
					
					if(!(tagSet.equals(""))){
						TagMap=getTagmap(tagSet);
						if(imageID==16){
							System.out.println("huuu");
							}
						//predictedRankWithTags=tagSimilarity.CalculateS_Similarity(tagVector, TagMap);
						predictedRankWithTags=tagSimilarity.calculateTagVectorSimilarity(tagVector, TagMap);
						predictedUserSimilarity=uSim.calculateUserSimilarity(userID, tempUserID);
						tempPredictedRanks=predictedRankWithTags*predictedUserSimilarity;
						System.out.println("UserSimilarity for "+tempImageID+" and UserID "+tempUserID+predictedUserSimilarity);
						System.out.println("TagSimilarity "+tempImageID+" "+predictedRankWithTags);
						System.out.println("TagBased Recommender with user similarity--"+tempPredictedRanks);
						Double rank=0.0d;
						
						
						if(myHashMap.containsKey(tempImageID))
						{
							rank=(Double)myHashMap.get(tempImageID);
							inserted=true;
						}
						else inserted=false;
				
						
						if(!inserted)
						{
							myHashMap.put(tempImageID, tempPredictedRanks);
						}
						else
						{
							Double newrank=tempPredictedRanks + rank.floatValue();
							myHashMap.put(tempImageID,newrank);
						}
						
						tagSet="";
					}
					tempImageID=imageID;
					tempUserID=longOtherUserID;
					//tagSet=tuples.getString("Tags");
				}
				if(tagSet.equals(""))
					tagSet+=tuples.getString("Tags");
				else
				tagSet+=","+tuples.getString("Tags");
			
			//System.gc();	
				
			}
			tuples.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	
			
		Iterator i=myHashMap.keySet().iterator();
		while(i.hasNext()){
			Integer key=(Integer)(i.next());
			System.out.println(key+"*=*"+myHashMap.get(key));
		}
		imageResultsMap=neighborhood.getFilteredImages(myHashMap);
		
		return imageResultsMap;

		

	}
	
	
	
	public HashMap<String, Integer> getTagmap(String tagString){
		String[] tagSet=tagString.split(",");
		HashMap<String, Integer> tmpTagVector=new HashMap<String, Integer>();
		for(int j=0;j<tagSet.length;j++){
			String tmpTag = tagSet[j];
			if (tmpTagVector.containsKey(tmpTag)) {
				tmpTagVector.put(tmpTag, tmpTagVector.get(tmpTag)+1);
			} else {
				tmpTagVector.put(tmpTag, 1);
			}
		}
		return tmpTagVector;
	}



		
}
